import { Page } from '@/base/Page';
import { deleteMember, getMemberList, getMemberSpecificList } from '@/base/api/MemberApi';

import { Member } from '@/base/model/Member';
import { MemberSpecific } from '@/base/model/MemberSpecific';
import { isEmpty } from '@/base/util/Strings';
import { getAgentList } from '@/base/api/AgentApi';
import { Agent } from '@/base/model/Agent';
import axios from 'axios';
import Cookies from 'js-cookie';

class Form {
  nickname = '';
  phone = '';
  sex = '';
  createBegin = '';
  createEnd = '';
  date = [];
  companyId = [];
  sortProperties = 'id';
  sortDirection = 'DESC';
  modelName = '';
  province = '';
  city = '';
  deviceSn = '';
  birthdayDate = undefined;
  sortPropertiesList = [
    {
      value: 'register_date',
      label: '注册时间'
    }
  ]
  sortList = [
    {
      value: 'ASC',
      label: '升序'
    },
    {
      value: 'DESC',
      label: '降序'
    },
  ]
  sexList = [
    {
      value: '1',
      label: '女'
    },
    {
      value: '0',
      label: '男'
    },
  ]
  monthList = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }, { value: 5 }, { value: 6 }, { value: 7 }, { value: 8 }, { value: 9 }, { value: 10 }, { value: 11 }, { value: 12 }]
}

export class ListBloc {
  form = new Form();

  // paged: Page<Member> = new Page<Member>([], 0, 1, 15, 0, []);
  paged: Page<MemberSpecific> = new Page<MemberSpecific>([], 0, 1, 15, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);
  loading = false;
  sortList: any = [];
  memberSelectArr: Array<MemberSpecific> = [];
  // memberSelectArr: Array<Member> = [];

  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.date[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.date[1].getTime() + 24 * 60 * 60 * 1000 || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getMemberSpecificList({
      pageIndex,
      pageSize,
      nickname: this.form.nickname,
      sex: this.form.sex,
      phone: this.form.phone,
      modelName: this.form.modelName,
      province: this.form.province,
      city: this.form.city,
      birthdayDate: this.form.birthdayDate,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd,
      sortProperties: this.form.sortProperties,
      sortDirection: this.form.sortDirection,
    }).then((res) => {
      this.paged = res.data;
      console.log(res.data, 'res.data')
    }).finally(() => {
      this.loading = false;
    });
    // return getMemberList({
    //   pageIndex,
    //   pageSize,
    //   phone: this.form.phone,
    //   createBegin: this.form.createBegin,
    //   createEnd: this.form.createEnd,
    //   companyId: this.form.companyId
    // }).then((res) => {
    //   console.log(res);
    //   this.paged = res.data;
    // }).finally(() => {
    //   this.loading = false;
    // });
  }
  downLoad() {
    const url = `${location.origin}/api/member/exportMemberList?pageIndex=${this.paged.pageIndex}&pageSize=${this.paged.pageSize}&nickname=${this.form.nickname}&sex=${this.form.sex}&phone=${this.form.phone}&modelName=${this.form.modelName}&province=${this.form.province}&city=${this.form.city}&createBegin=${this.form.createBegin}&createEnd=${this.form.createEnd}&sortProperties=id&sortDirection=DESC`
    const config = {
      responseType: 'blob',
      headers: {
        'Authorization': `Bearer ${Cookies.get('accessToken')}`
      },
    };
    axios
      .get(url, config)
      .then((response) => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'member.xlsx');
        document.body.appendChild(link);
        link.click();
      })
      .catch((error) => {
        console.error('导出失败', error);
      });
  }
  getAgentList(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    return getAgentList({
      pageIndex: this.angent.pageIndex,
      pageSize: 1000,
      sortDirection: 'ESC'
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = { id: 0, agentName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  removeItem(dateItem: Member) {
    this.loading = true;
    return deleteMember(dateItem.id)
      .finally(() => {
        this.loading = false;
      });
  }

  reset() {
    this.form.date = [];
    this.form.createBegin = '';
    this.form.createEnd = '';
    this.form.phone = '';
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.form.companyId = [];
    this.form.nickname = '';
    this.form.sex = '';
    this.form.sortProperties = 'id';
    this.form.sortDirection = 'DESC';
    this.form.modelName = '';
    this.form.province = '',
      this.form.city = '',
      this.form.birthdayDate = undefined,
      this.load(this.paged.pageIndex, this.paged.pageSize);
  }

  getCompanyName(companyId: number) {
    const item = this.paged.extras.find(x => x.id === companyId);
    return item == null ? '' : item.name;
  }
  getAgentName(agentId: number) {
    const item = this.sortList.find(x => x.id === agentId);
    return item == null ? '' : item.agentName;
  }
}
